package UILayer;

import ModelLayer.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LoginUI extends JFrame
{
	private final double VERSION = 1.0;
	private JPasswordField password_field;
	private JTextField txt_login;
	private MainMenuUI main_menu;
	private NewPanel panel;
	private boolean username;
	private boolean pass;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					MachineCollection.initialize(); 
					LeaseCollection.initialize();
					Stock.initialize();
					OrderCollection.initialize();
					CustomerCollection.initialize();
					StaffMemberCollection.initialize();
					JFrame frame = new LoginUI();
					frame.setResizable(false);
					frame.setVisible(true);
					

				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}
	
	public LoginUI()
	{
		super("Login");
		setIconImage(Toolkit.getDefaultToolkit().getImage(LoginUI.class.getResource("/UILayer/house567.ico")));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 339, 260);
		panel = new NewPanel(true);
		getContentPane().add(panel);
		panel.setLayout(null);
		username=false;
		pass=false;
		make_window();
		make_menu_bar();
	}

	private void make_window()
	{

		password_field = new JPasswordField();
		password_field.setFont(new Font("Tahoma", Font.PLAIN, 13));
		password_field.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent arg0) {
			if (!pass)
			{
				password_field.setForeground(Color.BLACK);
				password_field.setText("");
				pass=true;
			}
			}
		});
		password_field.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (!pass)
				{
					password_field.setForeground(Color.BLACK);
					password_field.setText("");
					pass=true;
				}
			}
		});
		password_field.setForeground(Color.GRAY);
		password_field.setHorizontalAlignment(SwingConstants.CENTER);
		password_field.setText("Password");
		password_field.setBounds(100, 66, 121, 30);
		panel.add(password_field);

		txt_login = new JTextField();
		txt_login.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txt_login.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent arg0) {
				if (!username)
				{
				txt_login.setForeground(Color.BLACK);
				txt_login.setText("");
				username=true;
				}
			}
		});
		txt_login.setHorizontalAlignment(SwingConstants.CENTER);
		txt_login.setForeground(Color.GRAY);
		txt_login.setText("Username");
		txt_login.setBounds(100, 25, 121, 30);
		panel.add(txt_login);
		txt_login.setColumns(10);

		JButton btn_ok = new JButton("Ok");
		btn_ok.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btn_ok.setBounds(10, 166, 89, 23);
		btn_ok.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				try_to_login(txt_login.getText(), password_field.getPassword());
			}
		});
		panel.add(btn_ok);

		JButton btn_exit = new JButton("Exit");
		btn_exit.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btn_exit.setBounds(224, 166, 89, 23);
		btn_exit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		panel.add(btn_exit);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
		requestFocusInWindow();

	}

	private void make_menu_bar()
	{
		JMenuBar menubar = new JMenuBar();
		JMenu menu;
		JMenuItem item;
		setJMenuBar(menubar);
		menu = new JMenu("Help");
		menubar.add(menu);
		item = new JMenuItem("About");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
				ActionEvent.CTRL_MASK));
		item.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				show_about();
			}
		});
		menu.add(item);
	}

	private void show_about()
	{
		JOptionPane.showMessageDialog(this, "Warehouse Software Beta for Vestbjerg Byggecenter A/S by Group4",
				"About", JOptionPane.INFORMATION_MESSAGE);
	}

	private void try_to_login(String login, char[] password)
	{
		String pass = String.copyValueOf(password);
		if ((CurrentAccount.change_account(StaffMemberCollection.find_account(
				login, pass))))
		{
			go_to_menu();
		}
		else
		{
			JOptionPane.showMessageDialog(this,
					"Invalid login or password.\nPlease try again.", null,
					JOptionPane.INFORMATION_MESSAGE);
			txt_login.setText(null);

			password_field.setText(null);
		}
	}

	private void go_to_menu()
	{
		main_menu = new MainMenuUI();
		main_menu.setVisible(true);
		dispose();
	}
}
